<?php

class UsersController extends AppController {

    var $name = 'Users';
    var $helpers = array('Html', 'Form', 'Session');
    var $components = array('Email');
    function beforeFilter() {
        parent::beforeFilter();
        $this->Auth->autoRedirect = true;
        $this->Auth->allow(array('login','admin_login','forget_password','admin_forget_password','logout','admin_logout','genRandomString'));
    }

    function index() {
        $this->User->recursive = 0;
        $this->set('users', $this->paginate());
    }

    function view($id = null) {
        if (!$id) {
            $this->Session->setFlash(__('Invalid user', true));
            $this->redirect(array('action' => 'index'));
        }
        $this->set('user', $this->User->read(null, $id));
    }

    function add() {
        if (!empty($this->data)) {
            $this->User->create();
            if ($this->User->save($this->data)) {
                $this->Session->setFlash(__('The user has been saved', true));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The user could not be saved. Please, try again.', true));
            }
        }
        $groups = $this->User->Group->find('list');
        $this->set(compact('groups'));
    }

    function edit($id = null) {
        if (!$id && empty($this->data)) {
            $this->Session->setFlash(__('Invalid user', true));
            $this->redirect(array('action' => 'index'));
        }
        if (!empty($this->data)) {
            if ($this->User->save($this->data)) {
                $this->Session->setFlash(__('The user has been saved', true));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The user could not be saved. Please, try again.', true));
            }
        }
        if (empty($this->data)) {
            $this->data = $this->User->read(null, $id);
        }
        $groups = $this->User->Group->find('list');
        $this->set(compact('groups'));
    }

    function delete($id = null) {
        if (!$id) {
            $this->Session->setFlash(__('Invalid id for user', true));
            $this->redirect(array('action' => 'index'));
        }
        if ($this->User->delete($id)) {
            $this->Session->setFlash(__('User deleted', true));
            $this->redirect(array('action' => 'index'));
        }
        $this->Session->setFlash(__('User was not deleted', true));
        $this->redirect(array('action' => 'index'));
    }

    function admin_index() {
        $this->User->recursive = 0;
        $this->set('users', $this->paginate());
    }

    function admin_view($id = null) {
        if (!$id) {
            $this->Session->setFlash(__('Invalid user', true));
            $this->redirect(array('action' => 'index'));
        }
        $this->set('user', $this->User->read(null, $id));
    }

    function admin_add() {
        if (!empty($this->data)) {
            $this->User->create();
            if ($this->User->save($this->data)) {
                $this->Session->setFlash(__('The user has been saved', true));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The user could not be saved. Please, try again.', true));
            }
        }
        $groups = $this->User->Group->find('list');
        $this->set(compact('groups'));
    }

    function admin_edit($id = null) {
        if (!$id && empty($this->data)) {
            $this->Session->setFlash(__('Invalid user', true));
            $this->redirect(array('action' => 'index'));
        }
        if (!empty($this->data)) {
            if ($this->User->save($this->data)) {
                $this->Session->setFlash(__('The user has been saved', true));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The user could not be saved. Please, try again.', true));
            }
        }
        if (empty($this->data)) {
            $this->data = $this->User->read(null, $id);
        }
        $groups = $this->User->Group->find('list');
        $this->set(compact('groups'));
    }

    function admin_login() {
        
    }
    function login() {
        
    }

    function genRandomString() {
        $length = 6;
        $characters = '0123456789abcdefghijklmnopqrstuvwxyz';
        $string = '';
        for ($p = 0; $p < $length; $p++) {
            $string .= $characters[mt_rand(0, strlen($characters) - 1)];
        }
        return $string;
    }

    function forget_password() {
        // $this->layout = FALSE;
        if (!empty($this->data)) {
            $chk = $this->User->find('count', array('conditions' => array('User.email' => $this->data['User']['email'])));
            if ($chk > 0) {
                $rand = $this->genRandomString();
                $users = $this->User->find('first', array('conditions' => array( 'User.email' => $this->data['User']['email']), 'fields' => array('User.username','User.id')));
                $this->User->id = $users['User']['id'];
                $this->User->saveField('password', $this->Auth->password($rand));
                $this->Email->to = $this->data['User']['email'];
                //$this->Email->bcc = array('secret@example.com');
                $this->Email->subject = 'Password of deListed database';
                $this->Email->replyTo = 'admin@delisted.com';
                $this->Email->from = 'deListed/Investogain <admin@delisted.com>';
                $this->Email->template = 'forgetpassword'; // note no '.ctp'
                //Send as 'html', 'text' or 'both' (default is 'text')
                $this->Email->sendAs = 'html'; // because we like to send pretty mail
                //Set view variables as normal
                $this->set('username', $users['User']['username']);
                $this->set('password', $rand);
                //Do not pass any args to send()
                $this->Email->send();
                $this->render('pass_success');
            } else {
                $this->Session->setFlash('Please provide correct information.');
            }
        }
    }
    function admin_forget_password() {
        // $this->layout = FALSE;
        if (!empty($this->data)) {
            $chk = $this->User->find('count', array('conditions' => array('User.username' => $this->data['User']['username'], 'User.email' => $this->data['User']['email'], 'User.dateofbirth' => $this->data['User']['dateofbirth'])));
            if ($chk > 0) {
                //$rand = $this->genRandomString();
                $users = $this->User->find('first', array('conditions' => array('User.username' => $this->data['User']['username'], 'User.email' => $this->data['User']['email'], 'User.dateofbirth' => $this->data['User']['dateofbirth']), 'fields' => array('User.id', 'User.hash_p'), 'recursive' => 0));
                $this->User->id = $users['User']['id'];
                //$this->User->saveField('password', $this->Auth->password($rand));
                $this->Email->to = $this->data['User']['email'];
                //$this->Email->bcc = array('secret@example.com');
                $this->Email->subject = 'Password of Authentic Avenue!';
                $this->Email->replyTo = 'noreply@authenticavenue.com';
                $this->Email->from = 'Authentic Team<info@authenticavenue.com>';
                $this->Email->template = 'forgetpassword'; // note no '.ctp'
                //Send as 'html', 'text' or 'both' (default is 'text')
                $this->Email->sendAs = 'html'; // because we like to send pretty mail
                //Set view variables as normal
                $this->set('username', $this->data['User']['username']);
                $this->set('password', $this->data['User']['hash_p']);
                //Do not pass any args to send()
                $this->Email->send();
                $this->render('pass_success');
            } else {
                $this->Session->setFlash('Please provide correct information.');
            }
        }
    }

    function logout() {
        $cookie = $this->Cookie->read('Auth.User');
        if (!is_null($cookie)) {
            $this->Cookie->delete('Auth.User');
        }
        $this->Session->setFlash('You have been successfully logged out from the system.');
        $this->redirect($this->Auth->logout());
    }
    function admin_logout() {
        $cookie = $this->Cookie->read('Auth.User');
        if (!is_null($cookie)) {
            $this->Cookie->delete('Auth.User');
        }
        $this->Session->setFlash('You have been successfully logged out from the system.');
        $this->redirect($this->Auth->logout());
    }

    function admin_delete($id = null) {
        if (!$id) {
            $this->Session->setFlash(__('Invalid id for user', true));
            $this->redirect(array('action' => 'index'));
        }
        if ($this->User->delete($id)) {
            $this->Session->setFlash(__('User deleted', true));
            $this->redirect(array('action' => 'index'));
        }
        $this->Session->setFlash(__('User was not deleted', true));
        $this->redirect(array('action' => 'index'));
    }

}
